package com.ohos.carbon.slice.demos;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.LogUtils;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class AutoCompleteSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();

        LogUtils.d("AutoCompleteSlice", "onStart");
        super.setUIContent(ResourceTable.Layout_ability_auto_complete);
        initView();

        LogUtils.d("Auto Complete", "onActive");

        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Auto Complete")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Auto Complete")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Auto Complete", integer);
                        pf.flushSync();
                        LogUtils.d("Auto Complete", "integer:" + integer);

                    }
                }
            } else {
                pf.putInt("Auto Complete", 1);
                pf.flushSync();
                LogUtils.d("Auto Complete", "integer:1");

            }

        } else {
            pf.putInt("Auto Complete", 1);
            pf.flushSync();
            LogUtils.d("Auto Complete", "else");

        }
        LogUtils.d("Auto Complete", "onActive end");
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Auto Complete");
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
